<?php

namespace app\api\controller;

use app\common\controller\Api;

use think\Db;

class Aps extends Api
{

    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    //http://fastmegoccr.test/api/Aps/resetplanlog
    public function resetplanlog()
    {
//        $wlist = \app\admin\model\aps\Workorder::all();
//        select * from fa_aps_workorder where finishdate > '2019-06-04'
        $workorder = new \app\admin\model\aps\Workorder;
        $wlist = $workorder
            ->where('finishdate', '>', date("Y-m"))
            ->select();

        foreach ($wlist as $row) {
            $planlog = \app\admin\model\aps\Planlog::get(['workorder_id' => $row->id]);
            if (!$planlog) {
                $planlog = new \app\admin\model\aps\Planlog;
                $planlog->workorder_id = $row->id;

//                $mainprocess = \app\admin\model\aps\Mainprocess::get($row->mainprocess_id);
                $procedure = new \app\admin\model\aps\Procedure;
                $procedurelist = $procedure->where('mainprocess_id', $row->mainprocess_id)->select();
                $needcaijie = 0;//是否需要拆解工序-- 在工单里标记
                $i = 0;
                foreach ($procedurelist as $rowp) {
                    if ($needcaijie == 0 && $rowp->name == "拆解") {
                        continue;
                    }
                    if ($i == 0) {
                        $planlog->procedure1_id = $rowp->id;
                        $planlog->procedure1_man_id = $this->getman_id($rowp->devicetype_id);
                        $planlog->procedure1_manhours = $rowp->manhours * $row->num;
                    } else if ($i == 1) {
                        $planlog->procedure2_id = $rowp->id;
                        $planlog->procedure2_man_id = $this->getman_id($rowp->devicetype_id);
                        $planlog->procedure2_manhours = $rowp->manhours * $row->num;
                    } else if ($i == 2) {
                        $planlog->procedure3_id = $rowp->id;
                        $planlog->procedure3_man_id = $this->getman_id($rowp->devicetype_id);
                        $planlog->procedure3_manhours = $rowp->manhours * $row->num;
                    } else if ($i == 3) {
                        $planlog->procedure4_id = $rowp->id;
                        $planlog->procedure4_man_id = $this->getman_id($rowp->devicetype_id);
                        $planlog->procedure4_manhours = $rowp->manhours * $row->num;
                    } else if ($i == 4) {
                        $planlog->procedure5_id = $rowp->id;
                        $planlog->procedure5_man_id = $this->getman_id($rowp->devicetype_id);
                        $planlog->procedure5_manhours = $rowp->manhours * $row->num;
                    } else if ($i == 5) {
                        $planlog->procedure6_id = $rowp->id;
                        $planlog->procedure6_man_id = $this->getman_id($rowp->devicetype_id);
                        $planlog->procedure6_manhours = $rowp->manhours * $row->num;
                    }
                    $i++;
                }

                if ($i > 1) {
                    $planlog->workorder_finishdate = $row->finishdate;//订单交货期
                    $planlog->maxnum = $i;
                    $planlog->save();
                }
            }

        }

        $this->success('插入成功');//code=1
    }

    function getman_id($devicetype_id)
    {

        $man = new \app\admin\model\aps\Man;
        $man_id = 0;
        $m = $man->whereOr('devicetype1_id', $devicetype_id)
            ->whereOr('devicetype2_id', $devicetype_id)
            ->whereOr('devicetype3_id', $devicetype_id)
            ->find();
        if ($m) {
            $man_id = $m->id;
        } else {
            $man_id = 0;
        }

        return $man_id;
    }

    //http://fastmegoccr.test/api/Aps/apsorder
    public function apsorder() {

//        $workman1 = [];
//        $a = array_fill(0, 2, "0");
//        $workman1[] = $a;
//        $a = array_fill(0, 2, "1");
//        $workman1[] = $a;
//        print_r($workman1);

        $nextworkday = $this->getnextworkday(date("Y-m-d"));
        $planlog = new \app\admin\model\aps\Planlog;

        $planloglist = $planlog
            ->where('maxnum', '<>', 'finishnum')
            ->where('procedure6_finishdate', '>', date("Y-m"))
            ->order('procedure6_finishdate asc, finishnum desc')
            ->field('id, procedure6_finishdate, finishnum, maxnum')
            ->select();

//工作时间 9:00～17：00  8小时，忽略午间休息
        foreach ($planloglist as &$row) {
            $row->procedure1_startdate = $nextworkday;
            $mlmodel = new \app\admin\model\aps\Manworklog;
            $manworklog = $mlmodel
                ->where('planlog_id', $row->id)
                ->where('man_id', $row->procedure1_man_id)
                ->where('procedure_id', $row->procedure1_id)
                ->order('id desc')
                ->limit(1)
                ->find();

            if (!$manworklog) {
                $mlmodel->planlog_id = $row->id;
                $mlmodel->man_id = $row->procedure1_man_id;
                $mlmodel->procedure_id = $row->procedure1_id;
                $mlmodel->index = 0;
                $mlmodel->procedure_manhours = $row->procedure1_manhours;
                $mlmodel->worktime_0 = "0～".$row->procedure1_manhours;
                $mlmodel->procedure_startdate = $nextworkday;
                $mlmodel->procedure_finishdate = $nextworkday;
                $mlmodel->save();
            } else {

            }

            $row->save();
            break;
        }


        $this->success('排单成功', $planloglist);//code=1
    }

    //获取下一个工作日 $thisday = '2019-10-1'
    function getnextworkday($thisday)
    {
//        date("Y-m-d",strtotime("+1 day"));
//        date( 'Y-m-d', strtotime('2018-10-1 +1 day') );

        for ($i = 1; $i < 7; $i++) {
//            $nextday = mktime(0, 0, 0, date("m"), date("d") + $i, date("Y"));
            $nextday = strtotime($thisday . ' +' . $i . ' day');
            $a = date("w", $nextday);
            if ($a == "0" || $a == "6") {
//                echo $nextday . "是周末";
            } else {
                $nextday = date("Y-m-d", $nextday);
                return $nextday;
            }
        }
    }

    //http://fastmegoccr.test/api/Aps/addvacation?name=昌&vacationdate=2019-07-08&workhour=0000000000111111
    //操作工请假记录
    //0~7工作时段 左->右
    public function addvacation()
    {
        $name = $this->request->request('name', '');
        if (empty($name)) {
            $this->error('错误的name!');
        }

        $man = \app\admin\model\aps\Man::get(['name' => $name]);
        if (empty($man)) {
            $this->error('查无此人!');
        }

        $vacationdate = $this->request->request('vacationdate', '');
        if (empty($vacationdate)) {
            $this->error('无请假日期!');
        }

        $workhour = $this->request->request('workhour', '');
        if (empty($workhour)) {
            $this->error('无请假时间!');
        }

        $manworkdate = \app\admin\model\aps\Manworkdate::where('man_id', $man->id)
            ->where('man_id', $man->id)
            ->where('workdate', $vacationdate)
            ->limit(1)
            ->find();//前缀匹配

        $workhour = base_convert($workhour, 2, 10);

        if (empty($manworkdate)) {
            $manworkdate = new \app\admin\model\aps\Manworkdate;
            $manworkdate->man_id = $man->id;
            $manworkdate->workdate = $vacationdate;
            $manworkdate->workhour = $workhour;
        } else {
            $manworkdate->workhour = $manworkdate->workhour | $workhour;
        }
        $manworkdate->save();

        $this->success('请假成功');

    }


    //http://fastmegoccr.test/api/Aps/getmanworkhour?name=昌&workdate=2019-07-08
    //操作工空闲状态获取
    public function getmanworkhour()
    {
        $name = $this->request->request('name', '');
        if (empty($name)) {
            $this->error('错误的name!');
        }

        $man = \app\admin\model\aps\Man::get(['name' => $name]);
        if (empty($man)) {
            $this->error('查无此人!');
        }

        $workdate = $this->request->request('workdate', '');
        if (empty($workdate)) {
            $this->error('无工作日期!');
        }

        $manworkdate = \app\admin\model\aps\Manworkdate::where('man_id', $man->id)
            ->where('man_id', $man->id)
            ->where('workdate', $workdate)
            ->limit(1)
            ->find();//前缀匹配

        $workhour = "0000000000000000";
        if (empty($manworkdate)) {
            $manworkdate = new \app\admin\model\aps\Manworkdate;
            $manworkdate->man_id = $man->id;
            $manworkdate->workdate = $workdate;
            $manworkdate->save();
        } else {
            $workhour = $manworkdate->workhour | $workhour;
            $workhour = base_convert($workhour, 10, 2).'';
        }

        $pre = "";
        for ($i = strlen($workhour); $i < 16; $i++) {
            $pre .= "0";
        }
        $workhour = $pre.$workhour;

        $this->success('操作工空闲状态获取成功', $workhour);

    }

    //http://fastmegoccr.test/api/aps/planlogAll?starttime=2019-08-26+08%3a30%3a00
    public function planlogAll($starttime)
    {
        set_time_limit(0); //执行时间无限
        ini_set('memory_limit', '-1'); //内存无限

        $starttime = urldecode($starttime);
//        $sql = "SELECT PL.`id`, PL.`workorder_id`, PD.`devicetype_id`, PL.`workorder_finishdate`, PD.`name` as PDName, PD.`manhours`, PL.`yjkg` as PLyjkg, WO.`name` as WOName, WO.`num`, PL.`finishnum`, PL.`maxnum`, PL.`overtime_hours`, PL.`procedure1_id`, PL.`procedure2_id`, PL.`procedure3_id`, PL.`procedure4_id`, PL.`procedure5_id`, PL.`procedure6_id`, PL.`procedure7_id`, PL.`procedure8_id`, PL.`procedure9_id`, PL.`procedure10_id`, PL.`procedure11_id`, PL.`procedure12_id`, PL.`procedure1_startdate`, PL.`procedure2_startdate`, PL.`procedure3_startdate`, PL.`procedure4_startdate`, PL.`procedure5_startdate`, PL.`procedure6_startdate`, PL.`procedure7_startdate`, PL.`procedure8_startdate`,  PL.`procedure9_startdate`, PL.`procedure10_startdate`, PL.`procedure11_startdate`, PL.`procedure12_startdate`,  PL.`procedure1_finishdate`, PL.`procedure2_finishdate`, PL.`procedure3_finishdate`, PL.`procedure4_finishdate`, PL.`procedure5_finishdate`, PL.`procedure6_finishdate`, PL.`procedure7_finishdate`, PL.`procedure8_finishdate`, PL.`procedure9_finishdate`, PL.`procedure10_finishdate`, PL.`procedure11_finishdate`, PL.`procedure12_finishdate`, PL.`procedure1_manhours`, PL.`procedure2_manhours`, PL.`procedure3_manhours`, PL.`procedure4_manhours`, PL.`procedure5_manhours`, PL.`procedure6_manhours`, PL.`procedure7_manhours`, PL.`procedure8_manhours`, PL.`procedure9_manhours`, PL.`procedure10_manhours`, PL.`procedure11_manhours`, PL.`procedure12_manhours`, PL.`procedure1_preparedate`, PL.`procedure2_preparedate`, PL.`procedure3_preparedate`, PL.`procedure4_preparedate`, PL.`procedure5_preparedate`, PL.`procedure6_preparedate`, PL.`procedure7_preparedate`, PL.`procedure8_preparedate`, PL.`procedure9_preparedate`, PL.`procedure10_preparedate`, PL.`procedure11_preparedate`, PL.`procedure12_preparedate` FROM fa_aps_planlog PL LEFT JOIN fa_aps_procedure PD on (PL.procedure1_id = PD.id or PL.procedure2_id = PD.id or PL.procedure3_id = PD.id or PL.procedure4_id = PD.id or PL.procedure5_id = PD.id or PL.procedure6_id = PD.id or PL.procedure7_id = PD.id or PL.procedure8_id = PD.id or PL.procedure9_id = PD.id or PL.procedure10_id = PD.id or PL.procedure11_id = PD.id or PL.procedure12_id = PD.id) LEFT JOIN fa_aps_workorder WO on PL.`workorder_id` = WO.`id` WHERE PL.`finishnum` < PL.`maxnum` AND WO.`name` LIKE '5101-%' AND (PL.`procedure1_preparedate` IS NULL OR '" . $starttime . "' < PL.`procedure1_preparedate`) OR (PL.`procedure2_preparedate` IS NULL OR '" . $starttime . "' < PL.`procedure2_preparedate`) OR (PL.`procedure3_preparedate` IS NULL OR '" . $starttime . "' < PL.`procedure3_preparedate`) OR (PL.`procedure4_preparedate` IS NULL OR '" . $starttime . "' < PL.`procedure4_preparedate`) OR (PL.`procedure5_preparedate` IS NULL OR '" . $starttime . "' < PL.`procedure5_preparedate`) OR (PL.`procedure6_preparedate` IS NULL OR '" . $starttime . "' < PL.`procedure6_preparedate`) OR (PL.`procedure7_preparedate` IS NULL OR '" . $starttime . "' < PL.`procedure7_preparedate`) OR (PL.`procedure8_preparedate` IS NULL OR '" . $starttime . "' < PL.`procedure8_preparedate`) OR (PL.`procedure9_preparedate` IS NULL OR '" . $starttime . "' < PL.`procedure9_preparedate`) OR (PL.`procedure10_preparedate` IS NULL OR '" . $starttime . "' < PL.`procedure10_preparedate`) OR (PL.`procedure11_preparedate` IS NULL OR '" . $starttime . "' < PL.`procedure11_preparedate`) OR (PL.`procedure12_preparedate` IS NULL OR '" . $starttime . "' < PL.`procedure12_preparedate`) ORDER by PL.`workorder_finishdate`, WO.`name`, PD.`id`";
        $sql = "SELECT PL.`id`, PL.`workorder_id`, PD.`devicetype_id`, PL.`workorder_finishdate`, PD.`name` AS PDName, PD.`manhours`, PL.`yjkg` as PLyjkg, WO.`name` AS WOName, WO.`num`, PL.`finishnum`, PL.`maxnum`, PL.`overtime_hours`, PL.`procedure1_id`, PL.`procedure2_id`, PL.`procedure3_id`, PL.`procedure4_id`, PL.`procedure5_id`, PL.`procedure6_id`, PL.`procedure7_id`, PL.`procedure8_id`, PL.`procedure9_id`, PL.`procedure10_id`, PL.`procedure11_id`, PL.`procedure12_id`, PL.`procedure1_startdate`, PL.`procedure2_startdate`, PL.`procedure3_startdate`, PL.`procedure4_startdate`, PL.`procedure5_startdate`, PL.`procedure6_startdate`, PL.`procedure7_startdate`, PL.`procedure8_startdate`,  PL.`procedure9_startdate`, PL.`procedure10_startdate`, PL.`procedure11_startdate`, PL.`procedure12_startdate`,  PL.`procedure1_finishdate`, PL.`procedure2_finishdate`, PL.`procedure3_finishdate`, PL.`procedure4_finishdate`, PL.`procedure5_finishdate`, PL.`procedure6_finishdate`, PL.`procedure7_finishdate`, PL.`procedure8_finishdate`, PL.`procedure9_finishdate`, PL.`procedure10_finishdate`, PL.`procedure11_finishdate`, PL.`procedure12_finishdate`, PL.`procedure1_manhours`, PL.`procedure2_manhours`, PL.`procedure3_manhours`, PL.`procedure4_manhours`, PL.`procedure5_manhours`, PL.`procedure6_manhours`, PL.`procedure7_manhours`, PL.`procedure8_manhours`, PL.`procedure9_manhours`, PL.`procedure10_manhours`, PL.`procedure11_manhours`, PL.`procedure12_manhours`, PL.`procedure1_preparedate`, PL.`procedure2_preparedate`, PL.`procedure3_preparedate`, PL.`procedure4_preparedate`, PL.`procedure5_preparedate`, PL.`procedure6_preparedate`, PL.`procedure7_preparedate`, PL.`procedure8_preparedate`, PL.`procedure9_preparedate`, PL.`procedure10_preparedate`, PL.`procedure11_preparedate`, PL.`procedure12_preparedate` FROM fa_aps_planlog PL LEFT JOIN fa_aps_procedure PD ON (PL.procedure1_id = PD.id OR PL.procedure2_id = PD.id OR PL.procedure3_id = PD.id OR PL.procedure4_id = PD.id OR PL.procedure5_id = PD.id OR PL.procedure6_id = PD.id OR PL.procedure7_id = PD.id OR PL.procedure8_id = PD.id OR PL.procedure9_id = PD.id OR PL.procedure10_id = PD.id OR PL.procedure11_id = PD.id OR PL.procedure12_id = PD.id) LEFT JOIN fa_aps_workorder WO ON PL.`workorder_id` = WO.`id` WHERE PL.`finishnum` < PL.`maxnum` AND WO.`name` LIKE '5101-%' AND (PL.`procedure1_preparedate` IS NULL OR  PL.`procedure1_preparedate` ='0000-00-00 00:00:00' OR '".$starttime."' < PL.`procedure1_preparedate`) OR (PL.`procedure2_preparedate` IS NULL OR PL.`procedure2_preparedate` ='0000-00-00 00:00:00' OR '".$starttime."' < PL.`procedure2_preparedate`) OR (PL.`procedure3_preparedate` IS NULL OR PL.`procedure3_preparedate` ='0000-00-00 00:00:00' OR '".$starttime."' < PL.`procedure3_preparedate`) OR (PL.`procedure4_preparedate` IS NULL OR PL.`procedure4_preparedate` ='0000-00-00 00:00:00' OR '".$starttime."' < PL.`procedure4_preparedate`) OR (PL.`procedure5_preparedate` IS NULL OR PL.`procedure5_preparedate` ='0000-00-00 00:00:00' OR '".$starttime."' < PL.`procedure5_preparedate`) OR (PL.`procedure6_preparedate` IS NULL OR PL.`procedure6_preparedate` ='0000-00-00 00:00:00' OR '".$starttime."' < PL.`procedure6_preparedate`) OR (PL.`procedure7_preparedate` IS NULL OR PL.`procedure7_preparedate` ='0000-00-00 00:00:00' OR '".$starttime."' < PL.`procedure7_preparedate`) OR (PL.`procedure8_preparedate` IS NULL OR PL.`procedure8_preparedate` ='0000-00-00 00:00:00' OR '".$starttime."' < PL.`procedure8_preparedate`) OR (PL.`procedure9_preparedate` IS NULL OR PL.`procedure9_preparedate` ='0000-00-00 00:00:00' OR '".$starttime."' < PL.`procedure9_preparedate`) OR (PL.`procedure10_preparedate` IS NULL OR PL.`procedure10_preparedate` ='0000-00-00 00:00:00' OR '".$starttime."' < PL.`procedure10_preparedate`) OR (PL.`procedure11_preparedate` IS NULL OR PL.`procedure11_preparedate` ='0000-00-00 00:00:00' OR '".$starttime."' < PL.`procedure11_preparedate`) OR (PL.`procedure12_preparedate` IS NULL OR PL.`procedure12_preparedate` ='0000-00-00 00:00:00' OR '".$starttime."' < PL.`procedure12_preparedate`) ORDER BY PL.`workorder_finishdate`, WO.`name`, PD.`id`";
            //执行插入操作
        $list = Db::query($sql);

        //判断是否执行成功
        if ($list) {
            $this->success('planlogAll ok!', $list);
        } else {
            $this->error('planlogAll error');
        }

    }

    //http://fastmegoccr.test/api/Aps/test
    public function test()
    {
        $aa = "2019-07 ~ 2019-09";
        $s = explode(" ~ ", $aa);

        $mpmodel = new \app\admin\model\aps\Mainprocess;
        $prmodel = new \app\admin\model\aps\Procedure;

        $mplist = $mpmodel
            ->where('state', 1)
            ->field('id, name, manhours')
            ->select();

        $list = [];
        foreach ($mplist as $row) {
            $prlist = collection($prmodel
                ->where('mainprocess_id', $row['id'])
                ->field('id, name, manhours')
                ->select())->toArray();

            $row['procedure'] = $prlist;
            $list[] = $row;
        }

//        $this->success('OK', $list[0]['procedure'][0]);
        $this->success('OK', $list[0]);

    }

    //每天0点统计一次，主制程的总工时和，工序的平均工时
    //http://fastmegoccr.test/api/Aps/tjall
    public function tjall() {
        set_time_limit(0); //执行时间无限
        ini_set('memory_limit', '-1'); //内存无限

        $mainprocesslist = \app\admin\model\aps\Mainprocess::all();
        foreach ($mainprocesslist as &$mainprocess) {
            $mainprocess->manhours = 0;
            $procedurelist = \app\admin\model\aps\Procedure::where('mainprocess_id', $mainprocess['id'])->select();
            foreach ($procedurelist as &$procedure) {
                $procedure->manhours = \app\admin\model\aps\Statistics::where('procedure_id', $procedure['id'])->avg('procedure_manhours');
                $mainprocess->manhours += $procedure->manhours;
                $procedure->save();
            }
            $mainprocess->save();
        }

        echo '<br>' . date('H:i:s') . '<br>';

    }

    //http://fastmegoccr.test/api/Aps/setpdday
    public function setpdday()
    {
        $pdday = date("Y-m-d");
        $vk = \app\admin\model\aps\Vk::get(1);
        $vk->v = $pdday;
        $vk->save();

        echo $pdday . '<br>';
    }


    //http://fastmegoccr.test/api/aps/reseterpdatasummary
    public function reseterpdatasummary($starttime)
    {
        set_time_limit(0); //执行时间无限
        ini_set('memory_limit', '-1'); //内存无限


    }

    //http://ant.test//api/aps/resetcxstate
    public function resetcxstate()
    {
        $ret = 0;
        $size = 0;
        $list = \app\admin\model\aps\Planlog::with(['workorder'])->where('cxstate', "0")->select();
        if ($list) {
            $size = count($list);
            foreach ($list as $row) {
                $order = \app\admin\model\aps\Order::get($row['workorder']['order_id']);
                if ($order) {
                    $prestr = substr($order['name'], 0, 4);
                    if ($prestr === "2216") {
                        $row['cxstate'] = "2";
                    } else {
                        $row['cxstate'] = "1";
                    }
                    $row->save();
                    $ret += 1;
                }
            }
        }

        echo 'ret = '.$ret." size=".$size;
    }

    //http://ant.test/api/aps/planlogset
    function planlogset()
    {
        set_time_limit(0); //执行时间无限
        ini_set('memory_limit', '-1'); //内存无限

        $ret = 0;
        $list = \app\admin\model\aps\Erpdatasummary::where('planlog_id', 0)->field('id, planlog_id, workorder_id')->select();
        $size = count($list);
        foreach ($list as &$row) {
            $planlog = \app\admin\model\aps\Planlog::where('workorder_id', $row->workorder_id)->field('id, workorder_id')->find();
            if ($planlog) {
                $ret++;
                $row->planlog_id = $planlog->id;
                $row->save();
            } else {
                $row->delete();
            }
        }
        echo 'ret = '.$ret." size=".$size;
    }

    //http://ant.test/api/aps/planlogset_workorder
    function planlogset_workorder()
    {
        set_time_limit(0); //执行时间无限
        ini_set('memory_limit', '-1'); //内存无限

        $ret = 0;
        $list = \app\admin\model\aps\Workorder::where('planlog_id', 0)->field('id, planlog_id')->select();
        $size = count($list);
        foreach ($list as &$row) {
            $planlog = \app\admin\model\aps\Planlog::where('workorder_id', $row->id)->field('id, workorder_id')->find();
            if ($planlog) {
                $ret++;
                $row->planlog_id = $planlog->id;
                $row->save();
            } else {
                $row->delete();
            }
        }
        echo 'ret = '.$ret." size=".$size;
    }

    //间隔1分钟
    //http://ant.test/api/aps/updatebomlog
    public function updatebomlog()
    {
        set_time_limit(0); //执行时间无限
        ini_set('memory_limit', '-1'); //内存无限

        //['0' => __('已開請購單'), '1' => __('有库存'), '2' => __('待进料'), '3' => __('已验收'), '4' => __('待验收')]
        $erpbomloglist = \app\admin\model\aps\Erpbomlog::where('rejectedquantity', 0)->where('zhidingjiean', '<>', '3')->select();

        foreach ($erpbomloglist as &$row) {
            $row->zhidingjiean = "3";
            $row->save();
        }
        unset($row);

        //第二個從fa_aps_erpdatasummary 中的每筆紀錄使用workorder_id 到fa_aps_erpbomlog 查詢出所有對應的workorder_id 紀錄，
        //所有的zhidingjiean 都是“1”or “3” , fa_aps_erpdatasummary  的 state 就設為“１”
        $eslist = \app\admin\model\aps\Erpdatasummary::where('state', 0)->select();
        foreach ($eslist as &$row) {
            $state = 1;
            $erpbomloglist = \app\admin\model\aps\Erpbomlog::where('workorder_id', $row->workorder_id)->select();
            foreach ($erpbomloglist as $row2) {
                $zhidingjiean = intval($row2->zhidingjiean);
                if ($zhidingjiean == 1 || $zhidingjiean == 3) {
                    $state = 1;
                } else {
                    $state = 0;
                    break;
                }
            }
            if ($state == 1) {
                $row->state = "1";
                $row->save();
            }
        }

        echo '<br>' . date('H:i:s') . '<br>';
    }

}
